import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:scrollview_observer/scrollview_observer.dart';
import 'package:ball_lion/entity/team/zq_team_schedule_entity.dart';
import 'package:ball_lion/server/api/soccer_api.dart';
import 'package:ball_lion/utils/date_time_utils.dart';
import 'package:ball_lion/widgets/bottom_picker_widget.dart';
import 'package:umeng_apm_sdk/umeng_apm_sdk.dart';

class SoccerTeamScheduleLogic extends GetxController {
  // SoccerTeamDetailLogic detail = Get.find(tag: "${Get.arguments}");
  int _yearIndex = 0;
  int _typeIndex = 0;
  List<TeamScheduleYearEntity>? _years;
  SoccerTeamScheduleEntity? _data;
  ScrollController scrollC = ApmScrollController();
  late ListObserverController observerController;
  List<MatchGroup>? _matchGroup = [];
  List<String> typeList = ["世界", "亚洲", "欧洲", "南美洲", "中北美洲"];

  int get typeIndex => _typeIndex;
  int get yearIndex => _yearIndex;
  SoccerTeamScheduleEntity? get data => _data;
  List<MatchGroup>? get matchGroup => _matchGroup;
  List<TeamScheduleYearEntity>? get years => _years;

  Future showDatePicker() async {
    _yearIndex = await Get.bottomSheet(BottomPickerWidget(
      _years?.map((e) => e.year!.toString()).toList() ?? [],
      title: '选择年份',
      initialIndex: _yearIndex,
    ));
    update();
  }

  void getType(index) {
    _typeIndex = index;
    if (_typeIndex != 0) {
      SoccerTeamScheduleEntity d =
          SoccerTeamScheduleEntity.fromJson(_data?.toJson() ?? {});
      d.matchGroup?.removeWhere((element) =>
          element.matchArray?.every(
              (e) => e.leagueId != _data?.leagueArray?[_typeIndex].leagueId) ??
          true);
      d.matchGroup?.forEach((element) {
        element.matchArray?.removeWhere(
            (e) => e.leagueId != _data?.leagueArray?[_typeIndex].leagueId);
      });
      _matchGroup = List.from(d.matchGroup ?? []);
    } else {
      _matchGroup = List.from(_data?.matchGroup ?? []);
    }
    update();
  }

  Future requestYears(int id) async {
    _years = await SoccerApi.getSoccerTeamScheduleYear(id);
    var index =
        _years?.indexWhere((element) => element.year == DateTime.now().year);
    if (index != -1 && index != null) _yearIndex = index;
    update();
  }

  Future requestData(int id) async {
    _data = await SoccerApi.getSoccerTeamSchedule(
        id, _years?[_yearIndex].year.toString() ?? "");
    _matchGroup = List.from(_data?.matchGroup ?? []);
    update();
  }

  void initObserver() {
    double offset = 0;
    var length = 0;
    int index = _data?.matchGroup?.indexWhere((element) =>
            element.title == DateTime.now().formatedString('yyyy-MM')) ??
        0;
    if (index != 0) {
      for (var i = 0; i < index; i++) {
        length = length + (_data?.matchGroup?[i].matchArray?.length ?? 0);
      }
      offset = 56.0 * index + 50 * length;
    }

    scrollC.jumpTo(offset);
  }

  String formScore(int type, MatchArray? entity) {
    if (type == 1) {
      switch (entity?.status) {
        case 5:
        case 6:
        case 7:
          if (entity?.homeScore90 != null && entity?.guestScore90 != null) {
            return "${entity!.homeScore90!}-${entity.guestScore90!}";
          } else {
            return "vs";
          }
        case 8:
          return "取消";
        case 9:
          return "中断";
        case 10:
          return "腰斩";
        case 11:
          return "延期";
        case 12:
          return "待定";
        default:
          return "vs";
      }
    } else {
      if (entity?.homeScorePk != null && entity?.guestScorePk != null) {
        return "点球 ${entity!.homeScorePk!}-${entity.guestScorePk!}";
      } else {
        if (entity?.homeScoreOt != null && entity?.guestScoreOt != null) {
          return "加时 ${entity!.homeScoreOt!}-${entity.guestScoreOt!}";
        } else {
          return "";
        }
      }
    }
  }
}
